/*
 * Copyright (c) 2010-2011, Andrea Palloni
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * - Neither the name of the copyright holder nor the names of its contributors may be used
 * 	  to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package net.palla.biblio.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Biblio extends Composite implements EntryPoint, ValueChangeHandler<String> {

	private static BiblioUiBinder uiBinder = GWT.create(BiblioUiBinder.class);
	interface BiblioUiBinder extends UiBinder<Widget, Biblio> { }

	@UiField LeftPanel leftPanel;
	@UiField SimplePanel centerPanel;
	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final BiblioServiceAsync biblioService = GWT.create(BiblioService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		RootLayoutPanel.get().add(uiBinder.createAndBindUi(this));
		String initToken = History.getToken();
	    if (initToken.length() == 0) {
	      History.newItem(HistoryTokens.MAIN);
	    }
		History.addValueChangeHandler(this);
		History.fireCurrentHistoryState();		
    }

	public void onValueChange(ValueChangeEvent<String> event) {
		//Here changes 
		String ht = event.getValue();
		if (HistoryTokens.MAIN.equals(ht)) {
			centerPanel.clear();
			return;
		}
		if (HistoryTokens.AUTHORS.equals(ht)) {
			//shows authors list in main page
			centerPanel.clear();
			AuthorListPanel authorList = new AuthorListPanel(biblioService);
			centerPanel.add(authorList);
			return;
		}
		if (HistoryTokens.CDD.equals(ht)) {
			//shows cdd List in main page
			centerPanel.clear();
			CddListPanel cddList = new CddListPanel(biblioService);
			centerPanel.add(cddList);
			return;
		}
		if (HistoryTokens.PUBLISHERS.equals(ht)) {
			//shows publishers List in main page
			centerPanel.clear();
			PublisherListPanel publisherList = new PublisherListPanel(biblioService);
			centerPanel.add(publisherList);
			return;
		}
	}

}
